<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改【请填写功能名称】')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-topic-edit" th:object="${sysTopic}">
            <input name="topicid" th:field="*{topicid}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">标题：</label>
                    <div class="col-sm-8">
                        <input name="title" th:field="*{title}" class="form-control" type="text">
                    </div>
                </div>
            </div>

            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">专题类型：</label>
                    <div class="col-sm-8">
                        <input name="typeid" th:field="*{typeid}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">专题封面：</label>
                    <div class="col-sm-8">
                        <input name="cover" th:field="*{cover}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
            <div class="form-group">
                <label class="col-xs-2 control-label">专题分类：</label>
                <div class="col-xs-4">
                    <select id="typeid" class="form-control">
                        <option th:each="post:${topic_type_array}" th:field="*{typeid}" th:value="${post.typeid}" th:text="${post.typename}" th:selected="${sysTopic.typeid}==${post.typeid}"  ></option>
                    </select>
                </div>
            </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">下载价格：</label>
                    <div class="col-sm-8">
                        <input name="downpay" th:field="*{downpay}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">打包下载折扣(0-1之间)：</label>
                    <div class="col-sm-8">
                        <input name="discount" th:field="*{discount}" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-4 control-label">推荐：</label>
                    <div class="col-sm-8">
                        <label class="toggle-switch switch-solid">
                            <input type="checkbox" id="isimp" th:checked="${sysTopic.isimp == 1 ? true : false}">
                            <span></span>
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-4 control-label">vip：</label>
                    <div class="col-sm-8">
                        <label class="toggle-switch switch-solid">
                            <input type="checkbox" id="isvip" th:checked="${sysTopic.isvip == 1 ? true : false}">
                            <span></span>
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-4 control-label">审核：</label>
                    <div class="col-sm-8">
                        <label class="toggle-switch switch-solid">
                            <input type="checkbox" id="state" th:checked="${sysTopic.state == 1 ? true : false}">
                            <span></span>
                        </label>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-4 control-label">下架：</label>
                    <div class="col-sm-8">
                        <label class="toggle-switch switch-solid">
                            <input type="checkbox" id="isdel" th:checked="${sysTopic.isdel == 1 ? true : false}">
                            <span></span>
                        </label>
                    </div>
                </div>

            <h4 class="form-header h4">文档数据</h4>
            <div class="row">
                <div class="col-sm-12">
                   <!-- <button type="button" class="btn btn-white btn-sm" onclick="addRow()"><i class="fa fa-plus"> 增加</i></button>-->
                    <button type="button" class="btn btn-white btn-sm" onclick="selectUsersToParentCallBack2()"><i class="fa fa-plus">新增</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/topic";
        $("#form-topic-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                var data = $('#form-topic-edit').serializeArray();
                var ids = [];
                $('.docids').each(function () {
                    var value = $(this).val();
                    ids.push(value);
                    //console.log(value);
                });
                if (ids.length == 0) {
                    $.modal.alertWarning("请选择文档数据");
                    return;
                }
                //console.log(data);
                //console.log(ids.join(","));
                data.push({"name": "docs", "value": ids.join(",")});
                var state = $("input[id='state']").is(':checked') == true ? 1 : 0;
                var isvip = $("input[id='isvip']").is(':checked') == true ? 1 : 0;
                var isimp = $("input[id='isimp']").is(':checked') == true ? 1 : 0;
                var isdel = $("input[id='isdel']").is(':checked') == true ? 1 : 0;
                var typeid = $.form.selectSelects("typeid");
                data.push({"name": "typeid", "value": typeid});
                data.push({"name": "state", "value": state});
                data.push({"name": "isvip", "value": isvip});
                data.push({"name": "isimp", "value": isimp});
                data.push({"name": "isdel", "value": isdel});
                $.operate.save(prefix + "/edit", data);
            }
        }

        function selectUsersToParentCallBack2(){
            var ids = [];
            $('.docids').each(function () {
                var value = $(this).val();
                ids.push(value);
                //console.log(value);
            });
            var options = {
                title: '选择文档',
                //url: prefix + "/doc?docs=[(${sysTopic.docs})]",
                url: prefix + "/doc?docs="+ids.join(","),
                callBack: doSubmit2
            };
            $.modal.openOptions(options);
        }
        function doSubmit2(index, layero){
            var rows = layero.find("iframe")[0].contentWindow.getSelections();
            if (rows.length == 0) {
                $.modal.alertWarning("请至少选择一条记录");
                return;
            }
            for (var i = 0; i < rows.length; i++) {
                var obj = $("#" + table.options.id).bootstrapTable('getData');
                var count = obj.length;
                var docsarr = [];
                obj.forEach(function(item) {
                    docsarr.push(item.id); // 输出每个对象的name属性
                });
                var row = {
                    index: $.table.serialNumber(count),
                    id:rows[i].id,
                    name: rows[i].name,
                }
                if(!docsarr.includes(rows[i].id))
                    sub.addRow(row);
            }
            //$('#userids').html('我是通过方式二来的：' + rows.join())

            $.modal.close(index);
        }
        function addRow() {
            var count = $("#" + table.options.id).bootstrapTable('getData').length;

            var row = {
                index: $.table.serialNumber(count),
                name: "",
                weight: "",
                price: "",
                date: "",
                type: "",
            }
            sub.addRow(row);
        }
        $(function() {
            // 初始化数据, 可以由后台传过来
            var data = [(${docs})];

            var options = {
                data: data,
                pagination: false,
                showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                columns: [{
                    checkbox: true
                },
                    {
                        field: 'index',
                        title: "序号",
                        formatter: function (value, row, index) {
                            var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                            var columnId = $.common.sprintf("<input class='docids'  type='hidden' id='goods[%s].id' value='%s'>", index, row.id);
                            return columnIndex + $.table.serialNumber(index) + columnId;
                        }
                    },

                    {
                        field: 'name',
                        title: '文档名称',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control goodsName disabled' type='text' id='goods[%s].name' value='%s'>", index, value);
                            return html;
                        }

                    },
                    {
                        title: '操作',

                        formatter: function (value, row, index) {
                            var value = $.common.isNotEmpty(row.index) ? row.index : $.table.serialNumber(index);
                            return '<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="sub.delRowByIndex(\'' + value + '\')"><i class="fa fa-remove"></i>删除</a>';
                        }
                    }]
            };
            $.table.init(options);
        })
    </script>
</body>
</html>